﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class SinWigglePlayableBehaviour : PlayableBehaviour
{
    Transform mTransform;
    Vector3 mCachePosition;
    float mAmp;
    float mFreq;
    float mDuration;
    float mBeginTick;
    float mCurrentTick;


    public void Init(Transform transform, float amp, float freq, float duration)
    {
        mTransform = transform;
        mAmp = amp;
        mFreq = freq;
        mDuration = duration;
    }

    public override void OnGraphStart(Playable playable)
    {
        base.OnGraphStart(playable);

        mBeginTick = Time.time;
        mCurrentTick = mBeginTick;

        mCachePosition = mTransform.position;
    }

    public override void OnGraphStop(Playable playable)
    {
        base.OnGraphStop(playable);

        mTransform.position = mCachePosition;
    }

    public override void PrepareFrame(Playable playable, FrameData info)
    {
        base.PrepareFrame(playable, info);

        if (mCurrentTick - mBeginTick <= mDuration)
        {
            mTransform.position += Vector3.one * Mathf.Sin(mCurrentTick * mFreq) * mAmp;
        }

        mCurrentTick += info.deltaTime;
    }
}
